package com.quanshu.exchange.ui.gui.shop_store;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.ui.base.BaseFragment;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.exchange.ui.gui.login.LoginActivity;
import com.quanshu.exchange.ui.gui.profile.ShoppingCartActivity;

import butterknife.BindView;

/**
 * Created by jye on 2017/12/6.
 */
public class ShopStoreFragment extends BaseFragment {

    @BindView(R.id.webView)
    WebView mWebview;
    WebSettings mWebSettings;
    private String indexUrl;
    private String oldUserId = "";

    public static ShopStoreFragment newInstance() {
        ShopStoreFragment f = new ShopStoreFragment();
        Bundle args = new Bundle();
        f.setArguments(args);
        return f;
    }

    @Override
    public int setFragmentLayoutID() {
        return R.layout.fragment_shop_store_layout;
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        initIndexUrl();
    }

    @Override
    public void onResume() {
        super.onResume();
        UserEntity userEntity = MyApplication.getInstance().getCurrentUser();
        if (oldUserId != null && userEntity != null && userEntity.getMemberid() != null) {
            if (!oldUserId.equals(userEntity.getMemberid())) {
                oldUserId = userEntity.getMemberid();
                initIndexUrl();
                return;
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        UserEntity userEntity = MyApplication.getInstance().getCurrentUser();
        if (oldUserId != null && userEntity != null && userEntity.getMemberid() != null) {
            if (!oldUserId.equals(userEntity.getMemberid())) {
                oldUserId = userEntity.getMemberid();
                initIndexUrl();
                return;
            }
        }
    }

    private void initIndexUrl() {
        String userId = null;
        String userMemberId = null;
        UserEntity userEntity = MyApplication.getInstance().getCurrentUser();
        if (userEntity != null) {
            userId = MyApplication.getInstance().getCurrentUser().getId() + "";
            userMemberId = MyApplication.getInstance().getCurrentUser().getMemberid();
        }
        if (userId != null && userMemberId != null) {
            indexUrl = Config.URL_SHOP_INDEX + "&user_id=" + userId +
                    "&memberid=" + userMemberId;
        } else {
            indexUrl = Config.URL_SHOP_INDEX;
        }
        mWebSettings = mWebview.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setDefaultTextEncodingName("UTF-8");
        mWebSettings.setTextSize(WebSettings.TextSize.NORMAL);
        mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebview.setWebChromeClient(new WebChromeClient());
        mWebview.setWebViewClient(new MyWebViewClient());

        mWebview.loadUrl(indexUrl);
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            LogUtil.print("==ShopStoreFragment===shouldOverrideUrlLoading====" + url);
            if (url.contains("index/login/1")) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                return true;
            }
            if (url.contains("goMyCart")) {
                UserEntity userEntity = MyApplication.getInstance().getCurrentUser();
                if (userEntity == null) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                    return true;
                }
                Intent intent = new Intent(getActivity(), ShoppingCartActivity.class);
                startActivity(intent);
                return true;
            } else {
                Intent intent = new Intent(getActivity(), ShopTwoActivity.class);
                intent.putExtra("url", url);
                startActivity(intent);
                return true;
            }
        }
    }

    public boolean onKeyDown(int keyCode) {
        if (!mWebview.canGoBack()) {
            return false;
        }
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebview.canGoBack()) {
            mWebview.goBack(); //goBack()表示返回WebView的上一页面
            return true;
        }
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}


